package cn.seqdata.util;

public final class BytesUtils {
	private static final char HEX[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
		'f'};

	private BytesUtils() {
	}

	public static String encode(byte[] bytes) {
		StringBuilder sb = new StringBuilder();

		for(byte b : bytes) {
			sb.append(HEX[(b & 0xf0) >> 4]);
			sb.append(HEX[b & 0x0f]);
		}

		return sb.toString();
	}

	public static byte[] decode(String hexstr) {
		byte[] result = new byte[hexstr.length() / 2];

		for(int i = 0; i < result.length; ++i) {
			int r0 = Character.digit(hexstr.charAt(i * 2 + 0), 16);
			int r1 = Character.digit(hexstr.charAt(i * 2 + 1), 16);

			result[i] = (byte) ((r0 << 4) | r1);
		}

		return result;
	}
}
